using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using System.Net.Http;
using System.Threading.Tasks;
using Xlit.Example;
using Xunit;

namespace Xlit.ExampleTest
{
    public class UnitTest1
    {
        private readonly HttpClient _client;
        public UnitTest1()
        {
            var builder = new WebHostBuilder().UseStartup<Startup>();
            var testServer = new TestServer(builder);
            _client = testServer.CreateClient();
        }
        [Fact]
        public async Task Test1()
        {
            var result = await _client.GetAsync("api/value");
            result.EnsureSuccessStatusCode();

            var data = await result.Content.ReadAsStringAsync();
            Assert.Equal("success", data);
        }
    }


    //[SetUp]
    //public void Init()
    //{
    //    var server = new TestServer(WebHost.CreateDefaultBuilder().UseStartup<Startup>());

    //    _deviceTest = server.Host.Services.GetService<DeviceTestController>();
    //}




    //// Arrange
    //var options = new DbContextOptionsBuilder<StoreDbContext>()
    //    .UseInMemoryDatabase(databaseName: "MyDb1")
    //    .Options;
    //var context = new StoreDbContext(options);
    //var repository = new EfRepository<Banner, int>(context);
    //var service = new BannerAppService(repository);
    //var controller = new BannerController(service);

    //// Act
    //var result = controller.Get();

    //// Assert
    //Assert.NotNull(result.Result);




    public class BasicTests
    : IClassFixture<WebApplicationFactory<Startup>>
    {
        private readonly WebApplicationFactory<Startup> _factory;

        public BasicTests(WebApplicationFactory<Startup> factory)
        {
            _factory = factory;
        }

        [Theory]
        [InlineData("/")]
        [InlineData("/Index")]
        [InlineData("/About")]
        [InlineData("/Privacy")]
        [InlineData("/Contact")]
        public async Task Get_EndpointsReturnSuccessAndCorrectContentType(string url)
        {
            // Arrange
            var client = _factory.CreateClient();

            // Act
            var response = await client.GetAsync(url);

            // Assert
            response.EnsureSuccessStatusCode(); // Status Code 200-299
            Assert.Equal("text/html; charset=utf-8",
                response.Content.Headers.ContentType.ToString());
        }
    }
}
